<?php

namespace app\common\model;

class Category extends BaseModel
{
    //获取一级栏目
    public function getNormalFirstCategory()
    {
        $data = [
            'status' => 1,
            'parent_id' => 0
        ];
        $order = [
            'id' => 'desc'
        ];
        return $this->where($data)->order($order)->select();
    }

    public function getFirstCategorys($parent_id = 0)
    {
        $data = [
            'parent_id' => $parent_id,
            'status' => ['neq', -1]
        ];
        $order = [
            'listorder' => 'desc',
            'id' => 'desc',
        ];
        $result = $this->where($data)->order($order)->select();
//        echo $this->getLastSql();
        return $result;
    }

    public function getNormalCategorysByParentId($parent_id)
    {
        if (empty($parent_id)) {
            return '';
        }
        $data = $this->where(['parent_id' => $parent_id])->select();
        return $data;
    }

    //获取一级分类并显示条数
    public function getNormalRecommendCategoryByParentId($parent_id = 0, $limit = 5)
    {
        $where = [
            'status' => 1,
            'parent_id' => $parent_id
        ];
        $order = [
            'listorder' => 'desc',
            'id' => 'desc'
        ];
        $result = $this->where($where)->order($order);
        if ($limit) {
            $result = $result->limit($limit);
        }
        $result = $result->select();
        return $result;
    }

    //获取父类的子分类
    public function getNormalCategoryByParentId($parent_id = [])
    {
        $where = [
            'parent_id' => ['in', $parent_id],
            'status' => 1
        ];
        $order = [
            'listorder' => 'desc',
            'id' => 'desc'
        ];
        return $this->where($where)->order($order)->select();
    }
}